package com.jiulan.hearthearing.ui.my.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jiulan.hearthearing.R;
import com.jiulan.hearthearing.base.BaseVPFragment;
import com.jiulan.hearthearing.constants.Constants;
import com.jiulan.hearthearing.constants.UrlConstants;
import com.jiulan.hearthearing.http.HttpRequest;
import com.jiulan.hearthearing.http.HttpStringCallBack;
import com.jiulan.hearthearing.ui.my.adapter.CoursesPurchasedAdapter;
import com.jiulan.hearthearing.ui.my.model.CoursePurchasedModle;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CoursesPurchasedFragment extends BaseVPFragment {
    private Context context;
    public static final String ARG_PAGE = "ARG_PAGE";
    private int mPage;
    private int page = 1;
    private String type = "";
    private CoursesPurchasedAdapter adapter;
    private List<CoursePurchasedModle.DataBean> data;

    private SmartRefreshLayout smartLayout;
    private RecyclerView rcvCourse;

    public static CoursesPurchasedFragment newInstance(int page) {
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, page);
        CoursesPurchasedFragment pageFragment = new CoursesPurchasedFragment();
        pageFragment.setArguments(args);
        return pageFragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mPage = getArguments().getInt(ARG_PAGE);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_courses_purchased, container, false);
        context = getActivity();
        initView(view);
        return view;
    }

    private void initView(View view) {
        smartLayout = view.findViewById(R.id.smartLayout);
        rcvCourse = view.findViewById(R.id.rcvCourse);
        rcvCourse.setLayoutManager(new LinearLayoutManager(context));
        switch (mPage) {
            case 0:
                type = "";
                break;
            case 1:
                type = "course";
                break;
            case 2:
                type = "bussiness";
                break;
            case 3:
                type = "bussiness";
                break;
        }
        setView();
    }

    private void setView() {
        data = new ArrayList<>();
        smartLayout.setEnableAutoLoadMore(true);//开启自动加载功能（非必须）
        smartLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        getData();
                    }
                }, 1000);
            }
        });
        smartLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page++;
                        getData();
                    }
                }, 1000);
            }
        });
    }

    @Override
    protected void onFragmentVisibleChange(boolean isVisible) {
        if (isVisible) {
            smartLayout.autoRefresh();
        }
    }

    private void getData() {
        Map<String, String> map = new HashMap();
        map.put("page", String.valueOf(page));//分页
        map.put("limit", String.valueOf(Constants.LIMIT));//分页
        map.put("type", type);//类型 默认全部
        new HttpRequest(context).doGet(UrlConstants.course_has_buy, null, map, CoursePurchasedModle.class, new HttpStringCallBack() {
            @Override
            public void onSuccess(Object result) {
                if (result instanceof CoursePurchasedModle) {
                    CoursePurchasedModle model = (CoursePurchasedModle) result;
                    if (page == 1) {
                        data.clear();
                        if (model.getData().size() > 0) {
//                            no_data.setVisibility(View.GONE);
                            data = model.getData();
                            adapter = new CoursesPurchasedAdapter(data);
                            rcvCourse.setAdapter(adapter);
                            if (model.getData().size() == Constants.LIMIT) {
                                //还有多的数据
                                smartLayout.finishRefresh();
                            } else {
                                //没有更多数据（上拉加载功能将显示没有更多数据）
                                smartLayout.finishRefreshWithNoMoreData();
                            }
                            adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                                @Override
                                public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                                    switch (view.getId()) {
                                        case R.id.tvCheck:
                                            switch (data.get(position).getType()) {
                                                case 0:
                                                    break;
                                                case 1:
                                                    break;
                                                case 2:
                                                    break;
                                                case 3:
                                                    break;
                                            }
                                            break;
                                    }
                                }
                            });
                        } else {
                            smartLayout.finishRefreshWithNoMoreData();
//                            no_data.setVisibility(View.VISIBLE);
                        }
                    } else {
                        data.addAll(model.getData());
                        adapter.notifyDataSetChanged();
                        if (model.getData().size() == Constants.LIMIT) {
                            //还有多的数据
                            smartLayout.finishLoadMore();
                        } else {
                            //没有更多数据（上拉加载功能将显示没有更多数据）
                            smartLayout.finishLoadMoreWithNoMoreData();//将不会再次触发加载更多事件
                        }
                    }
                }
            }

            @Override
            public void onFailure(int code, String msg) {

            }
        });
    }

    @Override
    protected void onFragmentFirstVisible() {

    }
}
